#include "room.h"

// 初始化房间(终端窗口)
void Room::init()
{
    system("mode con cols=100 lines=40");
    system("color 87");
    system("chcp 65001"); // windows�ն˱��� 936 ֧��GBK GB2312, 65001 ֧��utf-8
    srand(SEED);
    cout << s_clear;
}

// 加载数据
void Room::load()
{
    player = Actor();
    // 界面: 标题栏
    title.text = "\u2605 冒 险 世 界 v1.0 \u2605";
    title.bgcolor = s_bgyellow;

    myInfo.title = " ==== 玩家信息 ==== ";
    myInfo.bgcolor = s_bgblue;

    actorList.title = " ==== 互动角色 ==== ";
    actorList.bgcolor = s_bgred;

    msgs.title = " ========================= 消息栏 ========================= ";
    msgs.bgcolor = s_bggreen;

    eqptinfo.title = " ==== 装备信息 ==== ";
    eqptinfo.act = Room::player;
    eqptinfo.bgcolor = s_bggrass;

    packge.title = " ========================= 背包栏 ========================= ";
    packge.bgcolor = s_bgyellow;

    iteminfo.title = " ==== 物品信息 ==== ";
    iteminfo.bgcolor = s_bggrass;
    // 界面: 背景色
    inbox.bgcolor = s_bgblack;

    // 内容: 载入游戏内容
    Actor a, b;
    a.name = "Alan";
    b.name = "Tymon";
    actorList.add(&a);
    actorList.add(&b);
    Sword s("寒冰剑", 20);
    packge.add(s);
    player.hp = 100;
    player.name = s_black + "你" + s_white;
    msgs.push("欢迎加入冒险世界, " + player.name + " !");
}
// 更新数据内容游戏逻辑等
void Room::update()
{

    inbox.input();

    msgs.push(player.name + s_blue + ":" + s_white + inbox.text);

    if(inbox.func == "a"){
        Actor *target;
        target = actorList.a[inbox.idx];
        target->hide = true;
        fight(player,*target);
    }
}

//  刷新显示
void Room::refresh()
{
    //
    myInfo.update() ; 
    eqptinfo.update();
    page = title.refresh() + myInfo.refresh() + msgs.refresh()  + actorList.refresh() \
            + eqptinfo.refresh() + packge.refresh() + iteminfo.refresh();
    cout << page;
}


// 战斗(a vs b)
void Room::fight(Actor &a, Actor &b){
    while(true){
        int finalDam = 0;
        // b --> a
        finalDam = a.get_dam() - b.def;
        b.hp -= finalDam;
        msgs.push(a.name + " 攻击 " + b.name + " ,造成 " + s_red + to_string(finalDam) + msgs.color + " 点伤害." );
        // 击败判断
        if(b.hp < 0){
            msgs.push("玩家 " + b.name + " 被 " + a.name + " 击败.");
            b.hide = true;
            return;
        }
        // a --> b
        finalDam = b.get_dam() - a.def;
        a.hp -= finalDam;
        msgs.push(b.name + " 攻击 " + a.name + " ,造成 " + s_red + to_string(finalDam) + msgs.color + " 点伤害." );
        if(a.hp < 0){
            msgs.push("玩家 " + a.name + " 被 " + b.name + " 击败.");
            return;
        }


        refresh();
        delay(1000);
    }
}